package cn.wuxing.mediation.web.controller.common;

import cn.wuxing.mediation.common.core.domain.AjaxResult;
import cn.wuxing.mediation.common.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;

/**
 * 数据导入
 */
@Slf4j
@RestController
@RequestMapping("/import")
@RequiredArgsConstructor(onConstructor_ = @__({@org.springframework.beans.factory.annotation.Autowired}))
public class ImportDataController {
    private final ImportDataHandler importDataHandler;


    @GetMapping("/not_dispute_event")
    public AjaxResult not_dispute_event(@RequestParam("filePath") String filePath) throws IOException {
//        filePath="D:\\非纠纷模板.xlsx";
        File excelFile = new File(filePath);
        if(!excelFile.exists()) {
            throw new ServiceException("文件不存在");
        }
        //如果不是Excel文件直接抛出异常
        if(!excelFile.getName().endsWith(".xlsx")) {
            throw new ServiceException("非Excel文件");
        }
        return importDataHandler.importNonDisputeEvent(excelFile);
    }

    @GetMapping("/event_applicant")
    public AjaxResult event_applicant(@RequestParam("filePath") String filePath) throws IOException {
//        filePath="D:\\人员申请模板.xlsx";
        File excelFile = new File(filePath);
        if(!excelFile.exists()) {
            throw new ServiceException("文件不存在");
        }
        //如果不是Excel文件直接抛出异常
        if(!excelFile.getName().endsWith(".xlsx")) {
            throw new ServiceException("非Excel文件");
        }
        return importDataHandler.importEventApplicant(excelFile);
    }
}
